
package com.mp.chart.markerview;

import android.content.Context;
import android.text.Html;
import android.widget.TextView;

import com.mp.chart.data.Entry;
import com.mp.chart.data.MinutesData;
import com.mp.chart.formatter.DataFormatter;
import com.mp.chart.model.ADX;
import com.mp.chart.model.ATR;
import com.mp.chart.model.KDJ;
import com.mp.chart.model.MACD;
import com.mp.chart.model.RSI;
import com.mp.chart.model.VOL;
import com.mp.chart.utils.HqKlineDb;
import com.mp.chart.utils.VolumeUtils;
import com.android.R;

/**
 * 长按时BarChart右上角的view
 * Created by TenFace on 2017/9/26.
 */
public class IndexMarkerView extends ValueMarkerView {

    private final String difColor;
    private final String deaColor;
    private final String bollColor;
    private final String ubColor;
    private final String lbColor;
    private final String rsi1Color;
    private final String rsi2Color;
    private final String rsi3Color;
    private final String kColor;
    private final String dColor;
    private final String jColor;
    private final String macdColor;
    private final String adxColor;
    private final String atrColor;
    private final String textColor;
    private TextView tvContent;
    private HqKlineDb hqKlineDb;

    public IndexMarkerView(Context context, int layoutResource) {
        super(context, layoutResource);
        tvContent = (TextView) findViewById(R.id.tvContent);
        hqKlineDb = new HqKlineDb(context);
        this.textColor = DataFormatter.colorToString(getResources().getColor(R.color.text_bbb));
        macdColor = DataFormatter.colorToString(getResources().getColor(R.color.macd));
        difColor = DataFormatter.colorToString(getResources().getColor(R.color.dif));
        deaColor = DataFormatter.colorToString(getResources().getColor(R.color.dea));
        bollColor = DataFormatter.colorToString(getResources().getColor(R.color.boll));
        ubColor = DataFormatter.colorToString(getResources().getColor(R.color.ub));
        lbColor = DataFormatter.colorToString(getResources().getColor(R.color.lb));
        rsi1Color = DataFormatter.colorToString(getResources().getColor(R.color.rsi1));
        rsi2Color = DataFormatter.colorToString(getResources().getColor(R.color.rsi2));
        rsi3Color = DataFormatter.colorToString(getResources().getColor(R.color.rsi3));
        kColor = DataFormatter.colorToString(getResources().getColor(R.color.k));
        dColor = DataFormatter.colorToString(getResources().getColor(R.color.d));
        jColor = DataFormatter.colorToString(getResources().getColor(R.color.j));
        adxColor = DataFormatter.colorToString(getResources().getColor(R.color.adx));
        atrColor = DataFormatter.colorToString(getResources().getColor(R.color.atr));

    }

    @Override
    public void refreshContent(Entry e, int dataSetIndex, int pos) {
        Object data = e.getData();
        if (data instanceof MinutesData) {//柱状图
            showVOLValue((MinutesData) data);
        } else if(data instanceof VOL){
            showVOLValue((VOL) data);
        }
         else if (data instanceof MACD) {
            showMACDValue((MACD) data);
        } else if (data instanceof KDJ) {
            showKDJValue((KDJ) data);
        } else if (data instanceof RSI) {
            showRSIValue((RSI) data);
        } else if (data instanceof ADX) {
            showADXValue((ADX) data);
        } else if (data instanceof ATR) {
            showATRValue((ATR) data);
        }
    }


   /* private void showSARValue(SAR data) {
        String txt = String.format("<font color='%s'>SAR:%s</font>",
                sarColor, data.sar
        );
        tvContent.setText(Html.fromHtml(txt));

    }*/

    private void showVOLValue(MinutesData data) {
        String txt = String.format("<font color='%s'>成交量:%s</font>",
                textColor, VolumeUtils.getVolUnit(data.chengjiaoliang));
        tvContent.setText(Html.fromHtml(txt));
    }
    private void showVOLValue(VOL data) {
        String txt = String.format("<font color='%s'>成交量:%s</font>",
                textColor, VolumeUtils.getVolUnit(data.vol),
                rsi1Color, getValue(data.ma5),
                rsi2Color, getValue(data.ma10),
                rsi3Color, getValue(data.ma30)
        );
        tvContent.setText(Html.fromHtml(txt));
    }
    private void showRSIValue(RSI data) {
        String txt = String.format("<font color='%s'>RSI%s:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>RSI%s:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>RSI%s:%s</font>",
                rsi1Color, hqKlineDb.getRSI1(), getValue(data.rsi1),
                rsi2Color, hqKlineDb.getRSI2(), getValue(data.rsi2),
                rsi3Color, hqKlineDb.getRSI3(), getValue(data.rsi3)
        );
        tvContent.setText(Html.fromHtml(txt));

    }
/*

    private void showOBVValue(OBV data) {
        String txt = String.format("<font color='%s'>OBV:%s</font>",
                obvColor, data.obv
        );
        tvContent.setText(Html.fromHtml(txt));
    }
*/

    private void showKDJValue(KDJ data) {
        String txt = String.format("<font color='%s'>K:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>D:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>J:%s</font>",
                kColor, getValue(data.k),
                dColor, getValue(data.d),
                jColor, getValue(data.j)
        );
        tvContent.setText(Html.fromHtml(txt));
    }

    private void showADXValue(ADX data) {
        String txt = String.format("<font color='%s'>ADX:%s</font>",
                adxColor, getValue(data.adx)
        );
        tvContent.setText(Html.fromHtml(txt));
    }

    private void showATRValue(ATR data) {
        String txt = String.format("<font color='%s'>ATR:%s</font>",
                atrColor, getValue(data.atr)
        );
        tvContent.setText(Html.fromHtml(txt));
    }
    /*private void showBOLLValue(BOLL data) {
        String txt = String.format("<font color='%s'>UPPER:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>MID:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>LOWER:%s</font>",
                ubColor, data.ub,
                bollColor, data.boll,
                lbColor, data.lb
        );
        tvContent.setText(Html.fromHtml(txt));

    }

    private void showWRValue(WR data) {
        String txt = String.format("<font color='%s'>WR1:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>WR2:%s</font>",
                wr1Color, data.wr1,
                wr2Color, data.wr2
        );
        tvContent.setText(Html.fromHtml(txt));
    }

    private void showDMIValue(DMI data) {
        String txt = String.format("<font color='%s'>PDI:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>MDI:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>ADX:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>ADXR:%s</font>",
                pdiColor, data.pdi,
                mdiColor, data.mdi,
                adxColor, data.adx,
                adxrColor, data.adxr);
        tvContent.setText(Html.fromHtml(txt));

    }*/

    private void showMACDValue(MACD data) {

        String txt = String.format("<font color='%s'>DIFF:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>DEA:%s</font>&nbsp;&nbsp;" +
                        "<font color='%s'>MACD:%s</font>",
                difColor, getValue(data.dif),
                deaColor, getValue(data.dea),
                macdColor, getValue(data.macd));
        tvContent.setText(Html.fromHtml(txt));
    }

    private String getValue(float value) {
        String str;
        if (!Float.isNaN(value)) {
            str = DataFormatter.getFormat(value, "#0.00");
        } else {
            str = "--";
        }
        return str;
    }




}
